package com.aiguigu.crud.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.aiguigu.crud.bean.Employee;
import com.aiguigu.crud.bean.Msg;
import com.aiguigu.crud.service.EmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/*处理员工的增删改查*/
@Controller
public class EmployeeController {
	@Autowired
	EmployeeService employeeService;
	
	
	/* 单个批量二合一员工删除 */
	@RequestMapping(value = "/emp/{ids}",method = RequestMethod.POST)
	@ResponseBody
	public Msg deleteEmp(@PathVariable("ids")String ids) {
		if(ids.contains("-")) {
			//批量删除
			List<Integer> del_ids = new ArrayList<Integer>();
			String[] str_ids = ids.split("-");
			for (String string : str_ids) {
				del_ids.add(Integer.parseInt(string));
			}
			employeeService.deleteBatch(del_ids);
		}else {
			//单个删除
			Integer id = Integer.parseInt(ids);
			employeeService.deleteEmp(id);
		}
		return Msg.success();
	}
	
	
	/* 员工更新 */
	@RequestMapping(value = "/emp/{empId}",method = RequestMethod.PUT)
	@ResponseBody
	public Msg saveEmp(Employee employee) {
		//System.out.println("请求体中的值："+request.getParameter("gender"));
		employeeService.updateEmp(employee);
		return Msg.success();
	}
	
	/* 按照员工id查询员工 */
	@RequestMapping(value = "/emp/{id}",method = RequestMethod.GET)
	@ResponseBody
	public Msg getEmp(@PathVariable("id")Integer id) {
		Employee employee = employeeService.getEmp(id);
		return Msg.success().add("emp", employee);
	}
	
	
	
	/* 校验用户名是否可用 */
	@RequestMapping(value = "/checkuser")
	@ResponseBody
	public Msg checkuser(@RequestParam("empName")String empName) {
		boolean b = employeeService.checkUser(empName);
		//先判断用户名是否是合法的表达式
		String regx = "(^[a-zA-Z0-9_-]{4,16}$)|(^[\\u2E80-\\u9FFF]{2,6})";
		if(!empName.matches(regx)) {
			return Msg.fail().add("va_msg","用户名必须是2-6位中文或4-16英文和数字的组合");
		}
		
		//数据库用户名重复校验
		if(b) {
			return Msg.success();
		}else {
			return Msg.fail().add("va_msg","用户名不可用");
		}
	}
	
	/* 员工保存 */
	@RequestMapping(value = "/emp",method = RequestMethod.POST)
	@ResponseBody
	public Msg saveEmp(@Valid Employee employee,BindingResult result) {
		if(result.hasErrors()) {
			//校验失败，应该返回失败，在模态框中显示校验失败的错误信息
			Map<String, Object> map = new HashMap<String, Object>();
			List<FieldError> errors = result.getFieldErrors();
			for (FieldError fieldError : errors) {
				//System.out.println("错误的字段名："+fieldError.getField());
				//System.out.println("错误信息："+fieldError.getDefaultMessage());
				map.put(fieldError.getField(), fieldError.getDefaultMessage());
			}
			return Msg.fail().add("errorFields", map);
		}else {
			employeeService.saveEmp(employee);
			return Msg.success();
		}
	}
	
	
	
	
	

	/* 查询员工（分页查询） */
	
	@RequestMapping(value = "/emps")
	@ResponseBody
	public Msg getEmpsWithJson(@RequestParam(value = "pn",defaultValue = "1")Integer pn) {
			//引入PageHelper分页插件
				//在查询之前只需要调入,出入页码以及每页的数量
				PageHelper.startPage(pn, 5);
				//startPage后面紧跟着的这个查询就是一个分页查询
				List<Employee> emps = employeeService.getAll();
				//使用PageInfo包装查询后的结果
				PageInfo page = new PageInfo(emps,5);
				return Msg.success().add("PageInfo",page);
	}
	/*
	 * public String getEmps(@RequestParam(value = "pn",defaultValue = "1")Integer
	 * pn,Model model) { //引入PageHelper分页插件 //在查询之前只需要调入,出入页码以及每页的数量
	 * PageHelper.startPage(pn, 5); //startPage后面紧跟着的这个查询就是一个分页查询 List<Employee>
	 * emps = employeeService.getAll(); //使用PageInfo包装查询后的结果 PageInfo page = new
	 * PageInfo(emps,5); model.addAttribute("PageInfo", page); return "list"; }
	 */
}
